﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Caliburn.Micro;
using Coproject.Framework;
using Coproject.ViewModels.Interfaces;

namespace Coproject.ViewModels
{
    [Export(typeof (IShell))]
    public class ShellViewModel : Conductor<IModule>.Collection.OneActive, IShell
    {
        //Static because we initialise Logging statically to try to catch Caliburn.Micro's early messages
        public static BindableCollection<LocalTraceMessage> TraceMessages = new BindableCollection<LocalTraceMessage>();
        private readonly ILog _log;
        private bool _isBusy;

        [ImportingConstructor]
        public ShellViewModel([ImportMany] IEnumerable<Lazy<IModule, IModuleMetadata>> moduleHandles)
        {
            _log = LogManager.GetLog(typeof (ShellViewModel));
            _log.Info("Shell ViewModel starting");

            IsBusy = true;
            IEnumerable<IModule> modules = from h in moduleHandles orderby h.Metadata.Order select h.Value;
            Items.AddRange(modules);
            IsBusy = false;
        }

        //Import Creation is shared to provide an application wide 
        // BusyIndicator in shell.
        [Import(RequiredCreationPolicy = CreationPolicy.Shared)]
        public IBusyWatcher Busy { get; set; }

        [Import]
        public IWindowManager WindowManager { get; set; }

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                NotifyOfPropertyChange(() => IsBusy);
            }
        }
    }
}